
package com.jasperframework.boot.common.modules.base.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.jasperframework.boot.common.constant.JasperConstants;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;

/**
 * 树形结构实体父类
 * @author mazc@dibo.ltd
 * @version v3.0
 * @date 2022/10/12
 */
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
@Getter
@Setter
@Accessors(chain = true)
@Slf4j
public class BaseTreeEntity<T extends Serializable> extends BaseEntity<T> {
    private static final long serialVersionUID = 10205L;

    /**
     * 父级ID
     */
    private T parentId;

    public BaseTreeEntity<T> setParentId(T parentId) {
        this.parentId = parentId;
        return this;
    }

    /**
     * 父级ID的全路径
     * <p>
     * 格式：/([0-9a-f]+,)+/g
     */
    @JsonIgnore
    private String parentIdsPath;

    /**
     * 树节点的全名称
     * <p>
     * 格式：/([0-9a-f]+,)+/g
     */
    @JsonIgnore
    private String treeNames;

    private String treeName;

    /**
     * 父级排序的全路径
     * <p>
     * 格式：/([0-9a-f]+,)+/g
     */
    @JsonIgnore
    private String treeSorts;

    private String treeSort;

    private Integer treeLevel;

    /**
     * 初始化一个根节点
     * @param TREE_ROOT_ID 根节点值
     */
    protected void initRootTree(T TREE_ROOT_ID) {
        this.setParentId(TREE_ROOT_ID); // 根节点的 parentId 为 0
        this.setParentIdsPath(TREE_ROOT_ID + JasperConstants.SEPARATOR_COMMA); // 根节点的 parentIdsPath 为 0,
        this.setTreeSorts(StringUtils.isBlank(this.getTreeSort())
                ? StringUtils.EMPTY
                : this.getTreeSort() + JasperConstants.SEPARATOR_COMMA); // 根节点的 treeSorts 为 treeSort + ","
        this.setTreeNames(StringUtils.isBlank(this.getTreeName())
                ? StringUtils.EMPTY
                : this.getTreeName()); // 根节点的 treeNames 为 treeName
        this.setTreeLevel(0); // 根节点的层级为 0
    }

}
